﻿namespace Smart.Utils.UI.BarcodeLib
{
    using System;

    internal class UPCSupplement2 : BarcodeCommon, IBarcode
    {
        private string[] EAN_CodeA = new string[] { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
        private string[] EAN_CodeB = new string[] { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" };
        private string[] UPC_SUPP_2 = new string[] { "aa", "ab", "ba", "bb" };

        public UPCSupplement2(string input)
        {
            base.Raw_Data = input;
        }

        private string Encode_UPCSupplemental_2()
        {
            if (base.Raw_Data.Length != 2)
            {
                throw new Exception("EUPC-SUP2-1: Invalid data length. (Length = 2 required)");
            }
            if (!BarcodeEdit.CheckNumericOnly(base.Raw_Data))
            {
                throw new Exception("EUPC-SUP2-2: Numeric Data Only");
            }
            string str = "";
            try
            {
                str = this.UPC_SUPP_2[int.Parse(base.Raw_Data.Trim()) % 4];
            }
            catch
            {
                throw new Exception("EUPC-SUP2-3: Invalid Data. (Numeric only)");
            }
            string str2 = "1011";
            int num = 0;
            foreach (char ch in str)
            {
                char ch2;
                switch (ch)
                {
                    case 'a':
                        ch2 = base.Raw_Data[num];
                        str2 = str2 + this.EAN_CodeA[int.Parse(ch2.ToString())];
                        break;

                    case 'b':
                        ch2 = base.Raw_Data[num];
                        str2 = str2 + this.EAN_CodeB[int.Parse(ch2.ToString())];
                        break;
                }
                if (num++ == 0)
                {
                    str2 = str2 + "01";
                }
            }
            return str2;
        }

        public string Encoded_Value
        {
            get
            {
                return this.Encode_UPCSupplemental_2();
            }
        }
    }
}

